package com.depth.viewer.request.webservice
{
	import com.depth.viewer.request.PoolResponder;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.AsyncToken;
	import mx.rpc.soap.LoadEvent;
	import mx.rpc.soap.SOAPHeader;
	import mx.rpc.soap.WebService;
	
	public class SingleWebService 
	{

		public var service:WebService;
		private var _actions:ArrayCollection = new ArrayCollection();
		public function SingleWebService(wsdl:String)
		{
			service= new WebService();
			service.wsdl = wsdl;
			service.loadWSDL();
			service.addEventListener(LoadEvent.LOAD,wsdlLoadedHandle);
		}
		
		public function request(pool:PoolResponder,methodName:String,args:Array,header:SOAPHeader=null):void{
			if(service.ready){
				if(header){
					service.clearHeaders();
					service.addHeader(header);
				}
				var _token:AsyncToken;
				switch(args.length){
					case 0:
						_token = service.getOperation(methodName).send();
						break;
					case 1:
						_token = service.getOperation(methodName).send(args[0]);
						break;
					case 2:
						_token = service.getOperation(methodName).send(args[0],args[1]);
						break;
					case 3:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2]);
						break;
					case 4:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3]);
						break;
					case 5:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4]);
						break;
					case 6:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4],args[5]);
						break;
					case 7:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
						break;
					case 8:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
						break;
					case 9:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
						break;
					case 10:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
						break;
				}
				_token.addResponder(pool);
			}else{
				_actions.addItem({methodName:methodName,params:args,pool:pool});
				service.loadWSDL();
			}
		}
		
		private function wsdlLoadedHandle(event:LoadEvent):void{
			event.target.removeEventListener(LoadEvent.LOAD,wsdlLoadedHandle);
			var actions:Array = _actions.toArray();
			while(actions.length > 0){
				var action:Object = actions.shift();
				request(action.pool,action.methodName,action.params);
			}
		}


	}
}